<?php

namespace app\admin\controller;

use app\common\model\GroupModel;
use app\common\model\PermitModel;
use think\Controller;
use think\Db;

class Group extends Intercept
{
    public function index()
    {
        try {
            return $this->fetch();
        } catch (\Exception $e) {
            echo $e->getMessage() . '----------' . $e->getLine();
        }
    }

    public function getList()
    {
        try {
            $res = Db::name('cms_blog_group')->order(['id' => 'desc'])->select();
            $data = [];
            foreach ($res as $v) {
                $data[] = [
                    'id' => $v['id'],
                    'mark' => $v['mark'],
                    'name' => $v['name'],
                    'description' => $v['description'],
                ];
            }
            return ["code" => 0, "msg" => "", "count" => 1000, "data" => $data];
        } catch (\Exception $e) {
            echo $e->getMessage() . '----------' . $e->getLine();
        }
    }


    public function add()
    {
        try {
            $data = Db::name('cms_blog_menu')->select();
            $list = [];
            foreach ($data as $arr) {
                $list[$arr['parent']][] = $arr;
            }
            $this->assign('list', $list);
            return $this->fetch('add');
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    public function edit($id)
    {
        try {
            $data = Db::name('cms_blog_group')->where('id', $id)->find();
            $dataMenu = Db::name('cms_blog_menu')->column('id,name,parent');
            $permit = json_decode($data['center_permit']);
            $str = '';
            foreach ($permit as $val){
                $str .= $val.',';
            }

            $list  = [];
            foreach ($dataMenu as $arr) {
                $list[$arr['parent']][] = $arr;
            }

            $this->assign('permit', rtrim($str,','));
            $this->assign('list', $list);
            $this->assign('data', $data);
            return $this->fetch('edit');
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }

    public function updateData()
    {
        try {
            if (request()->isPost()) {
                $params = input('param.');
                $gModel = new GroupModel();
                $returnData = $gModel->updateData($params);
                return $returnData;
            }
        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }


}
